package com.tsl.reggie.service.impl;

import com.tsl.reggie.common.R;
import com.tsl.reggie.domain.dto.SetmealDto;
import com.tsl.reggie.domain.po.Setmeal;
import com.tsl.reggie.domain.po.SetmealDish;
import com.tsl.reggie.mapper.SetmealMapper;
import com.tsl.reggie.service.ISetmealDishService;
import com.tsl.reggie.service.ISetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author 唐书亮
 * @since 2024-01-20
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {
    @Autowired
    private ISetmealDishService setmealDishService;
    @Override
    public R<String> saveSetmealDish(SetmealDto setmealDto) {
        //保存套餐信息
        save(setmealDto);
        //处理套餐菜品关联表里的套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes  =   setmealDishes.stream().map((item) ->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品信息关联表
        setmealDishService.saveBatch(setmealDishes);
        return R.success("保存成功");
    }
}
